//
//  LZZSandboxViewerVC.m
//  LZZSandboxViewer
//
//  Created by luzz on 2021/11/2.
//

#import "LZZSandboxViewerVC.h"
#import "LSBFileListVC.h"
#import "LSBFileListCell.h"
#import "LSBFileModel.h"


@interface LZZSandboxViewerVC ()
@property(nonatomic,strong)NSArray <LSBFileModel *>* list;
@end

@implementation LZZSandboxViewerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"沙盒";
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[LSBFileListCell class] forCellReuseIdentifier:NSStringFromClass([LSBFileListCell class])];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.list.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSBFileListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LSBFileListCell class])];
    cell.model = self.list[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSBFileModel * model = self.list[indexPath.row];
    LSBFileListVC * vc = [[LSBFileListVC alloc] initWithPath:model.filePath];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - lazy

- (NSArray<LSBFileModel *> *)list
{
    if (!_list) {
        NSString * documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
        NSString * libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES)[0];
        NSString * cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];
        NSString * tmpPath = NSTemporaryDirectory();
        
        LSBFileModel * document = [[LSBFileModel alloc] initWithFilePath:documentPath];
        LSBFileModel * lib = [[LSBFileModel alloc] initWithFilePath:libPath];
        LSBFileModel * caches = [[LSBFileModel alloc] initWithFilePath:cachesPath];
        LSBFileModel * tmp = [[LSBFileModel alloc] initWithFilePath:tmpPath];
        
        _list = @[document,lib,caches,tmp];
    }
    return _list;
}

@end
